package com.dding.cms.base.web.widget.handler;

import static com.dding.cms.base.web.ActionConstants.CONTENT_TABLE_NAME;
import static com.dding.cms.base.web.ActionConstants.RELEASEDATE;
import static com.dding.cms.base.web.ActionConstants.WIDGET_FIELF_SEPERATOR;
import static com.dding.cms.base.web.ActionConstants.WIDGET_TYPE_DATATIME;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dding.core.util.StringUtils;

import com.dding.cms.base.web.widget.IWidgetContext;
import com.dding.cms.base.web.widget.Widget;
import com.dding.cms.base.web.widget.WidgetField;

@Widget(Id = WIDGET_TYPE_DATATIME)
@WidgetField(values = { CONTENT_TABLE_NAME + WIDGET_FIELF_SEPERATOR
		+ RELEASEDATE })
public class ReleaseDataHandler extends AbstractWidgetHandler {

	private SimpleDateFormat format = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	@Override
	public Object handleSet(IWidgetContext widgetContext) {
		String[] result = new String[2];
		result[0] = RELEASEDATE;
		String[] value = (String[]) widgetContext.getParameters().get(
				RELEASEDATE);
		if (value == null || value.length == 0 || StringUtils.isEmpty(value[0])) {
			result[1] = format.format(new Date());
		} else {
			result[1] = value[0];
		}
		return result;
	}
}
